import Taro from '@tarojs/taro'
import { myTopics, getTopicList, topicFollow, getTopicTrack, cleanTopicTrack } from '../../services/topic'
import { parseTopic } from '../../utils/parse'

const namespace = 'page_topic'
export { namespace }
export default {
  namespace,
  state: {
    state: 0,
    hasMore: true,
    isLoadData: false,
    tracksList: [],
    items: [],
    recommends: [],
    pageIndex: 1,
    pageSize: 15
  },
  effects: {
    *init(_, { select, call, put }) {
      let pageSize = yield select((state) => state[namespace].pageSize)
      let pageIndex = yield select((state) => state[namespace].pageIndex)
      try {
        let res = yield call(myTopics, 1, pageSize)
        // console.log(res)
        if (res) {
          if (res.data.length > 0) {
            const items = parseTopic(res.data)
            console.log(items)
            // const items = res.data.map((item) => {
            //   item.hasFollow = false
            //   item.iconPath = QI_NIU_YUN_URL + item.iconPath
            //   return item
            // })
            const index = pageIndex + 1
            yield put({
              type: 'setState',
              payload: {
                items: items,
                state: 3,
                pageIndex: index
              }
            })
          } else {
            res = yield call(getTopicList, 1, 10)
            if (res) {
              const recommends = parseTopic(res.data.rows)
              console.log(recommends)
              // const recommends = res.data.rows.map((item) => {
              //   item.hasFollow = false
              //   item.iconPath = QI_NIU_YUN_URL + item.iconPath
              //   return item
              // })
              //console.log(recommends)
              yield put({
                type: 'setState',
                payload: {
                  recommends: recommends,
                  state: 3
                }
              })
            } else {
              yield put({
                type: 'setState',
                payload: {
                  state: 1
                }
              })
            }
          }
        } else {
          yield put({
            type: 'setState',
            payload: {
              state: 1
            }
          })
        }
      } catch (error) {}
    },
    *loadTrack(_, { put }) {
      const tracks = getTopicTrack()
      yield put({
        type: 'setState',
        payload: {
          tracksList: tracks
        }
      })
    },
    *loadData(_, { select, call, put }) {
      const isLoadData = yield select((state) => state[namespace].isLoadData)
      const hasMore = yield select((state) => state[namespace].hasMore)
      let pageSize = yield select((state) => state[namespace].pageSize)
      let pageIndex = yield select((state) => state[namespace].pageIndex)
      const items = yield select((state) => state[namespace].items)
      if (isLoadData || !hasMore) {
        return
      }
      yield put({
        type: 'setState',
        payload: {
          isLoadData: true
        }
      })
      try {
        const res = yield call(myTopics, pageIndex, pageSize)
        if (res) {
          const more = res.data.rows === pageSize
          const index = pageIndex + 1
          yield put({
            type: 'setState',
            payload: {
              items: [...items, ...res],
              pageIndex: index,
              hasMore: more
            }
          })
        }
      } catch (error) {}
      yield put({
        type: 'setState',
        payload: {
          isLoadData: false
        }
      })
    },
    *onFollow(payload, { select, call, put }) {
      let recommendList = yield select((state) => state[namespace].recommends)
      const item = payload.payload.item
      const index = payload.payload.index
      try {
        const res = yield call(topicFollow, item.id)
        // console.log(res)
        if (res.code === 0) {
          console.log(item)
          if (item.hasFollow) {
            Taro.showToast({
              title: '已取消关注',
              icon: 'success',
              duration: 2000
            })
          } else {
            Taro.showToast({
              title: '关注成功,刷新页面可查看关注话题',
              icon: 'success',
              duration: 2000
            })
          }
          recommendList[index].hasFollow = !item.hasFollow
          // let tempItem = recommendList[index]
          // tempItem.hasFollow = !item.hasFollow
          // recommendList[index] = tempItem
          console.log(recommendList[index])
          // TODO: bug待修复 dva 数组里面待对象的属性改变了，视图没更新
          // recommendList = recommendList.slice(4,8)
          yield put({
            type: 'setState',
            payload: {
              recommends: recommendList
            }
          })
          // yield put({
          //   type: 'setRecoecommends',
          //   payload: {
          //     index: index,
          //     item: item
          //   }
          // })
        } else {
          Taro.showToast('关注失败,重试')
        }
      } catch (error) {}
    },
    *cleanTopicTrack(_, { put }) {
      cleanTopicTrack()
      yield put({
        type: 'setState',
        payload: {
          tracksList: []
        }
      })
    }
  },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    },
    setRecoecommends(state, { payload }) {
      let newState = Object.assign({}, state)
      newState.recommends[payload.index].hasFollow = !payload.item.hasFollow
      console.log(newState)
      return newState
    }
  }
}
